IF exists(SELECT * FROM dbo.sysobjects WHERE name='geefKlassenVakken' AND xtype='FN') 
BEGIN
	DROP FUNCTION geefKlassenVakken
END
GO

create function geefKlassenVakken
(@klas varchar(5))
returns @resultaat table (klas varchar(5),vak char(2))
begin
declare @vakken table(vak char(2))

insert into @vakken
select vakcode from leraars..ambtsverdeling
where klas = @klas

declare deCursor cursor local for 
  select vak
  from @vakken
for read only

open deCursordeclare @vak char(2)

fetch next from deCursor into @vak
while @@fetch_status=0
begin  insert into @resultaat(vak)
  values(@vak)
  fetch next from deCursor into @vak
end

deallocate deCursor

return
end